package cn.enilu.flash.api.controller.cust;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.entity.cust.CustInfo;
import cn.enilu.flash.bean.entity.park.ParkingInfo;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.GunsException;
import cn.enilu.flash.bean.vo.front.ErrorMessage;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.conf.properties.LocalFileProperties;
import cn.enilu.flash.enums.body.State;
import cn.enilu.flash.service.cust.CustInfoService;
import cn.enilu.flash.utils.BeanUtil;
import cn.enilu.flash.utils.ToolUtil;
import cn.enilu.flash.utils.factory.Page;
import cn.enilu.flash.warpper.CustInfoWarpper;
import cn.enilu.flash.warpper.ParkInfoWarpper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cust")
public class CustInfoController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private LocalFileProperties localFileProperties;
	@Autowired
	private CustInfoService custInfoService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list(@RequestParam(required = false) String mobile,
					   @RequestParam(required = false) String name,
					   @RequestParam(required = false) String cardNo) {
		Page<CustInfo> page = new PageFactory<CustInfo>().defaultPage();
		page.addFilter("mobile", SearchFilter.Operator.EQ, mobile);
		page.addFilter("name", SearchFilter.Operator.EQ, name);
		page.addFilter("cardNo", SearchFilter.Operator.EQ, cardNo);
		page = custInfoService.queryPage(page);
		List list = (List) new CustInfoWarpper(BeanUtil.objectsToMaps(page.getRecords()), localFileProperties).warp();
		page.setRecords(list);
		return Rets.success(page);
	}
	@RequestMapping(method = RequestMethod.POST)
	@BussinessLog(value = "编辑客户信息", key = "name",dict= CommonDict.class)
	public Object save(@ModelAttribute CustInfo custInfo){
		if(custInfo.getId()==null){
			CustInfo tmp = custInfoService.get(new SearchFilter("mobile", SearchFilter.Operator.EQ, custInfo.getMobile()));
			if(tmp != null){
				return Rets.failure(ErrorMessage.DUPLICATE_CUST_MOBILE);
			}
			custInfoService.insert(custInfo);
		}else {
			custInfoService.update(custInfo);
		}
		return Rets.success();
	}
	@RequestMapping(method = RequestMethod.DELETE)
	@BussinessLog(value = "删除客户信息", key = "id",dict= CommonDict.class)
	public Object remove(Long id){
		if (ToolUtil.isEmpty(id)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		custInfoService.delete(id);
		return Rets.success();
	}

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Object get(@PathVariable("id") Long id){
		if(id != null && id > 0){
			CustInfo info = custInfoService.get(id);
			if(info == null){
				return Rets.failure("客户信息不存在");
			}else{
				return Rets.success(new CustInfoWarpper(BeanUtil.beanToMap(info), localFileProperties).warp());
			}
		}else{
			return Rets.failure("客户信息不存在");
		}
	}
}